<template>
    <div class="list-wrapper">
        <div class="list-top">
            <el-form :inline="true" :model="searchForm">
                <el-form-item label="类别">
                    <el-input v-model="searchForm.type" placeholder="请输入类别"></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input v-model="searchForm.asset_name" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="存放位置">
                    <el-select v-model="searchForm.location" placeholder="请选择" clearable>
                        <el-option v-for="item in location" :label="item" :value="item" :key="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search()">搜索</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="">
            <table class="list-table" border="0" cellspacing="0" cellpadding="0">
                <thead>
                <tr>
                    <th width="10%">
                        <el-button type="text" @click="openAdd()"><span class="icon add"></span></el-button>
                    </th>
                    <th width="5%">序号</th>
                    <th>资产编号</th>
                    <th>类别</th>
                    <th>名称</th>
                    <th>存放位置</th>
                    <th>现库存量</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>
                        <span class="icon del" @click="Del(item.id)"></span>
                        <span class="icon det" @click="onDetail(item.id)"></span>
                    </td>
                    <td>{{index + 1}}</td>
                    <td>{{item.asset_num}}</td>
                    <td>{{item.type}}</td>
                    <td>{{item.asset_name}}</td>
                    <td>{{item.location}}</td>
                    <td>{{item.inventory}}</td>
                </tr>
                </tbody>
            </table>
            <div class="page">
                <div class="block">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next, jumper"
                                   :total="pageTotal">
                    </el-pagination>
                </div>
            </div>
            <el-dialog title="办公用品信息" :visible.sync="pop" size="tiny">
                <el-form :model="addForm" :label-width="width" :rules="officeRules" ref="officeRulesName">
                    <el-form-item label="资产编号" prop="asset_num">
                        <el-col :span="span">
                            <el-input v-model="addForm.asset_num" size="small" placeholder="请输入资产编号"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="资产名称" prop="asset_name">
                        <el-col :span="span">
                            <el-input v-model="addForm.asset_name" size="small" placeholder="请输入资产名称"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="存放位置">
                        <el-col :span="span">
                            <el-select v-model="addForm.location" placeholder="请选择">
                                <el-option v-for="item in location" :label="item" :value="item" :key="item"></el-option>
                            </el-select>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="类型">
                        <el-col :span="span">
                            <el-input v-model="addForm.type" size="small" placeholder="请输入类型"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="现库存量">
                        <el-col :span="span">
                            <el-input v-model="addForm.inventory" size="small" placeholder="请输入现库存量"></el-input>
                        </el-col>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="pop = false">取 消</el-button>
                    <el-button type="primary" @click="change()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    const office = {
        asset_num: '',
        type: '',
        asset_name: '',
        location: '',
        inventory: ''
    };
    export default {
        data() {
            return {
                location: ['三楼仓库', '八楼仓库', '八楼存储柜'],
                searchForm: {
                    type: '',
                    asset_name: '',
                    location: '',
                },

                width: '200px',
                span: 15,
                pageIndex: 1,
                pageTotal: 1,
                list: {},

                pop: false,
                addForm: _g.cloneJson(office),
                officeRules: {
                    asset_name: [{required: true, message: "请填写资产名称", trigger: "blur"}],
                    asset_num: [{required: true, message: "请填写资产编号", trigger: "blur"}]
                },


            }
        },

        created() {
            this.getOfficeList();
        },

        methods: {
            search() {
                this.getOfficeList();
            },

            handleCurrentChange(val) {
                this.pageIndex = val;
                this.getOfficeList();
            },

            onDetail(id) {
                router.push('/operate/assets/officeDetail/' + id);
            },

            /* 获取数据 */
            getOfficeList() {
                let data = this.searchForm;
                data.page = this.pageIndex;
                this.apiPost("Operation/officeList", data).then(res => {
                    this.list = res.list;
                    this.pageTotal = parseInt(res.total);
                });
            },

            /* 打开新增窗口 */
            openAdd() {
                this.addForm = _g.cloneJson(office);
                this.pop = true;
                this.$refs.officeRulesName.resetFields();
            },

            /* 提交数据 */
            change() {
                this.$refs.officeRulesName.validate(valid => {
                    if (valid) {
                        this.pop = false;
                        let data = this.addForm;
                        this.apiPost("Operation/officeAdd", data).then(res => {
                            let remind = res.error ? "error" : "success";
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getOfficeList();
                            } else {
                                this.pop = true;
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },

            /* 删除 */
            Del(id) {
                this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        let data = {id: id};
                        this.apiPost("Operation/officeDelete", data).then(res => {
                            if (!res.error) {
                                this.$message({
                                    type: "success",
                                    message: res.msg
                                });
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                });
                            }
                            this.getOfficeList();
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
        }
    }
</script>

<style scoped>
    .add {
        width: 25px;
        height: 25px;
        background: url(../../../img/add.png) no-repeat;
        background-size: 100%;
        cursor: pointer;
    }

    .edit {
        background: url(../../../img/edit.gif) no-repeat;
    }

    .del {
        background: url(../../../img/del.png) no-repeat;
    }

    .det {
        background: url(../../../img/detail.png) no-repeat;
        cursor: pointer;
    }

</style>
